﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include <QMenuBar>
#include <QAction>
#include <QPushButton>
#include <QWidget>
#include <QObject>
#include <QPainter>
#include <QMouseEvent>
#include <QVector>
#include <QRect>
#include <QDebug>
#include <QtMath>


MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
    resize(680, 480);
    this->setWindowTitle(u8"画图板");
    QIcon icon(":/new/pictures/windowlogo.jpg");
    this->setWindowIcon(icon);

    pix = new QPixmap(size());
    pix->fill(Qt::white);
    save = false;

    QMenuBar *bar = menuBar();
    this->setMenuBar(bar);

    QMenu *SaveMenu = bar->addMenu(u8"保存");
    QMenu *EditMenu = bar->addMenu(u8"编辑");
    QMenu *PaintMenu = bar->addMenu(u8"绘图");

    QAction *move = new QAction(u8"移动", this);
    EditMenu->addAction(move);
    QAction *zoomOut = new QAction(tr(u8"放大"), this);
    EditMenu->addAction(zoomOut);
    QAction *zoomIn = new QAction(tr(u8"缩小"), this);
    EditMenu->addAction(zoomIn);
    QAction *clear = new QAction(tr(u8"清除"), this);
    EditMenu->addAction(clear);

    QAction *CurveAction = new QAction(tr(u8"曲线"), this);
    PaintMenu->addAction(CurveAction);
    QAction *LineAction = new QAction(tr(u8"直线"), this);
    PaintMenu->addAction(LineAction);
    QAction *RectAction = new QAction(tr(u8"矩形"), this);
    PaintMenu->addAction(RectAction);
    QAction *CircleAction = new QAction(tr(u8"圆"), this);
    PaintMenu->addAction(CircleAction);
    QAction *EllipseAction = new QAction(tr(u8"椭圆"), this);
    PaintMenu->addAction(EllipseAction);

    connect(LineAction, SIGNAL(triggered(bool)), this, SLOT(Line()));
    connect(RectAction, SIGNAL(triggered(bool)), this, SLOT(Rect()));
    connect(CircleAction, SIGNAL(triggered(bool)), this, SLOT(Circle()));
    connect(EllipseAction, SIGNAL(triggered(bool)), this, SLOT(Ellipse()));
    connect(clear, SIGNAL(triggered(bool)), this, SLOT(ClearPainter()));
    connect(zoomOut,SIGNAL(triggered(bool)),this,SLOT(zoomOut()));
    connect(zoomIn,SIGNAL(triggered(bool)),this,SLOT(zoomIn()));
    connect(move,SIGNAL(triggered(bool)),this,SLOT(moved()));
}

void MainWindow::mousePressEvent(QMouseEvent *event) {
    start = event->pos();
    selectedShape = nullptr; // ������ǰѡ�е�ͼ��
    save = true;


    // �������������Ƿ���ͼ���ڲ�
    for (Shape &shape : shapes) {
        if (shape.rect.contains(start)) {
            shape.isSelected = true;
        } else {
            shape.isSelected = false;
        }
    }
    update();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
    end = event->pos();
    if (save) {
        if (selectedShapeType == Shape::Line) {
            shapes.append(Shape(Shape::Line, QRect(start, end), selectedLineColor));
        } else if (selectedShapeType == Shape::Rectangle) {
            shapes.append(Shape(Shape::Rectangle, QRect(start, end), selectedLineColor));
        } else if (selectedShapeType == Shape::Circle) {
            int r = qSqrt(qPow(end.x() - start.x(), 2) + qPow(end.y() - start.y(), 2));
           shapes.append(Shape(Shape::Circle, QRect(start.x() - r, start.y() - r, 2 * r, 2 * r), selectedLineColor));
        } else if (selectedShapeType == Shape::Ellipse) {
            shapes.append(Shape(Shape::Ellipse, QRect(start, end), selectedLineColor));
        }
        update();
    }
    save = false;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event) {
    end = event->pos();
    update();
}

void MainWindow::paintEvent(QPaintEvent *event) {
    QPainter p(pix);
    for (const Shape &shape : shapes) {
        //p.setPen(QPen(shape.lineColor));
        if(shape.isSelected){p.setPen(QPen(Qt::red, 3));}
        else{p.setPen(QPen(Qt::black, 1));}
        if (shape.type == Shape::Line) {
            p.drawLine(shape.rect.topLeft(), shape.rect.bottomRight());
        } else if (shape.type == Shape::Rectangle){
            p.drawRect(shape.rect);
        } else if (shape.type == Shape::Circle) {
            p.drawEllipse(shape.rect);
        } else if (shape.type == Shape::Ellipse) {
            p.drawEllipse(shape.rect);
        }
    }

    QPainter px(this);
    px.drawPixmap(0, 0, *pix);
    if (selectedShapeType == Shape::Line) {
        px.setPen(QPen(selectedLineColor));
        px.drawLine(start, end);
    } else if (selectedShapeType == Shape::Rectangle) {
        px.setPen(QPen(selectedLineColor));
        px.drawRect(QRect(start, end));
    } else if (selectedShapeType == Shape::Circle) {
        int r = qSqrt(qPow(end.x() - start.x(), 2) + qPow(end.y() - start.y(), 2));
        px.setPen(QPen(selectedLineColor));
        px.drawEllipse(QRect(start.x() - r, start.y() - r, 2 * r, 2 * r));
    } else if (selectedShapeType == Shape::Ellipse) {
        px.setPen(QPen(selectedLineColor));
        px.drawEllipse(QRect(start, end));
    }
}

void MainWindow::Line() {
    selectedShapeType = Shape::Line;
}

void MainWindow::Rect() {
    selectedShapeType = Shape::Rectangle;
}

void MainWindow::Circle() {
    selectedShapeType = Shape::Circle;
}

void MainWindow::Ellipse() {
    selectedShapeType = Shape::Ellipse;
}

void MainWindow::ClearPainter() {
    selectedShapeType = Shape::None;
    pix->fill(Qt::white);
    shapes.clear();
    update();
}

void MainWindow::zoomOut()
{
    QVector<Shape> shapesNew = shapes;
    for(int i = shapes.count() - 1;i >= 0;--i){
        if(shapes.at(i).isSelected){//只放大选中的图形

            //重新计算图形矩形
            QRect rect = shapes.at(i).rect;
            QPointF leftTop = rect.topLeft();
            QPointF rightBottom = rect.bottomRight();
            double rate = (rightBottom.x() - leftTop.x()) / (rightBottom.y() - leftTop.y());//计算矩形长宽比
            int zoomPixel = 10;//放大因子,想要修改放大倍数修改这个参数
            QPointF leftTopNew(leftTop.x() - zoomPixel,leftTop.y() - zoomPixel/rate);
            QPointF rightBottomNew(rightBottom.x()+zoomPixel,rightBottom.y()+zoomPixel/rate);
            QRect rectNew(leftTopNew.toPoint(),rightBottomNew.toPoint());

            selectedShapeType = shapes.at(i).type;

            //添加放大后的图形
            if (selectedShapeType == Shape::Line) {
                shapesNew.append(Shape(Shape::Line, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Rectangle) {
                shapesNew.append(Shape(Shape::Rectangle, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Circle) {
                int r = qSqrt(qPow(rightBottomNew.x() - leftTopNew.x(), 2) + qPow(rightBottomNew.y() - leftTopNew.y(), 2));
                shapesNew.append(Shape(Shape::Circle, QRect(leftTopNew.x() - r, leftTopNew.y() - r, 2 * r, 2 * r), selectedLineColor));
            } else if (selectedShapeType == Shape::Ellipse) {
                shapesNew.append(Shape(Shape::Ellipse, rectNew, selectedLineColor));
            }

            shapesNew.removeAt(i);//移除原有选中图形

        }
    }

    //更新画板
    ClearPainter();
//    update();
    shapes = shapesNew;
    update();
}

void MainWindow::zoomIn()
{
    QVector<Shape> shapesNew = shapes;
    for(int i = shapes.count() - 1;i >= 0;--i){
        if(shapes.at(i).isSelected){//只缩小选中的图形

            //重新计算图形矩形
            QRect rect = shapes.at(i).rect;
            QPointF leftTop = rect.topLeft();
            QPointF rightBottom = rect.bottomRight();
            double rate = (rightBottom.x() - leftTop.x()) / (rightBottom.y() - leftTop.y());//计算矩形长宽比
            int zoomPixel = 10;//缩小因子,想要修改放大倍数修改这个参数
            QPointF leftTopNew(leftTop.x() + zoomPixel,leftTop.y() + zoomPixel/rate);
            QPointF rightBottomNew(rightBottom.x()-zoomPixel,rightBottom.y()-zoomPixel/rate);
            QRect rectNew(leftTopNew.toPoint(),rightBottomNew.toPoint());

            selectedShapeType = shapes.at(i).type;

            //添加缩小后的图形
            if (selectedShapeType == Shape::Line) {
                shapesNew.append(Shape(Shape::Line, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Rectangle) {
                shapesNew.append(Shape(Shape::Rectangle, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Circle) {
                int r = qSqrt(qPow(rightBottomNew.x() - leftTopNew.x(), 2) + qPow(rightBottomNew.y() - leftTopNew.y(), 2));
                shapesNew.append(Shape(Shape::Circle, QRect(leftTopNew.x() - r, leftTopNew.y() - r, 2 * r, 2 * r), selectedLineColor));
            } else if (selectedShapeType == Shape::Ellipse) {
                shapesNew.append(Shape(Shape::Ellipse, rectNew, selectedLineColor));
            }

            shapesNew.removeAt(i);//移除原有选中图形

        }
    }

    //更新画板
    ClearPainter();
    //    update();
    shapes = shapesNew;
    update();
}

void MainWindow::moved()
{
    class::move* m = new class::move;
    m->connect(m,SIGNAL(Move(int,int)),this,SLOT(onMove(int,int)));
    m->exec();
    m->deleteLater();
}

void MainWindow::onMove(int x, int y)
{
    QVector<Shape> shapesNew = shapes;
    for(int i = shapes.count() - 1;i >= 0;--i){
        if(shapes.at(i).isSelected){//只移动选中的图形

            //重新计算图形矩形
            QRect rect = shapes.at(i).rect;
            QPointF leftTop = rect.topLeft();
            QPointF rightBottom = rect.bottomRight();

            QPointF leftTopNew(leftTop.x() + x,leftTop.y() + y);
            QPointF rightBottomNew(rightBottom.x()+x,rightBottom.y()+y);
            QRect rectNew(leftTopNew.toPoint(),rightBottomNew.toPoint());

            selectedShapeType = shapes.at(i).type;

            //添加缩小后的图形
            if (selectedShapeType == Shape::Line) {
                shapesNew.append(Shape(Shape::Line, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Rectangle) {
                shapesNew.append(Shape(Shape::Rectangle, rectNew, selectedLineColor));
            } else if (selectedShapeType == Shape::Circle) {
                int r = qSqrt(qPow(rightBottomNew.x() - leftTopNew.x(), 2) + qPow(rightBottomNew.y() - leftTopNew.y(), 2));
                shapesNew.append(Shape(Shape::Circle, QRect(leftTopNew.x() - r, leftTopNew.y() - r, 2 * r, 2 * r), selectedLineColor));
            } else if (selectedShapeType == Shape::Ellipse) {
                shapesNew.append(Shape(Shape::Ellipse, rectNew, selectedLineColor));
            }

            shapesNew.removeAt(i);//移除原有选中图形

        }
    }

    //更新画板
    ClearPainter();
    //    update();
    shapes = shapesNew;
    update();
}

MainWindow::~MainWindow() {
    delete ui;
}
